package com.woniuxy01;

import java.sql.*;
import java.util.List;
import java.util.Scanner;

/**
@author 作者
@create 2025-11-18 10:43
*/public class Test02 {
    public static void main(String[] args) throws Exception {
        List<Stu> stus = JdbcUtils.executeQueryList(Stu.class, "select * from stu");
        System.out.println(stus);

        List<Teacher> teachers = JdbcUtils.executeQueryList(Teacher.class, "select * from teacher");
        System.out.println(teachers);

        Stu stu = JdbcUtils.executeQueryOne(Stu.class, "select * from stu where sid=?", 11);
        System.out.println(stu);
    }

    private static void test04() throws ClassNotFoundException, SQLException {
        String className="com.mysql.cj.jdbc.Driver";
        String url="jdbc:mysql:///db1";
        String user="root";
        String password="123";
        Class.forName(className);
        Connection connection = DriverManager.getConnection(url, user, password);
        String sql="select * from stu ";
        PreparedStatement statement = connection.prepareStatement(sql);
        ResultSet resultSet = statement.executeQuery();
        //获取元数据：描述数据的数据
        ResultSetMetaData metaData = resultSet.getMetaData();
        //getColumnCount()获取总列数
        for(int i=0;i<metaData.getColumnCount();i++){
            //获取列名
            System.out.println(metaData.getColumnLabel(i+1));
            System.out.println(metaData.getColumnName(i+1));
        }

//        while(resultSet.next()){
//            System.out.println(resultSet.getObject(1));
//            System.out.println(resultSet.getObject(2));
//            System.out.println(resultSet.getObject(3));
//            System.out.println(resultSet.getObject(4));
//            System.out.println("-----------------");
//        }
        resultSet.close();
        statement.close();
        connection.close();
    }

    private static void test03() throws Exception {
        Scanner scanner=new Scanner(System.in);
        System.out.println("输入学号");
        int sid=scanner.nextInt();
        System.out.println("输入姓名");
        String sname=scanner.next();
        System.out.println("输入密码");
        String pwd=scanner.next();
        System.out.println("输入年龄");
        int age=scanner.nextInt();
        String sql="insert into stu values(?,?,?,?)";
        JdbcUtils.executeUpdate(sql,sid,sname,pwd,age);
//        JdbcUtils.executeUpdate(sql,new Object[]{sid,sname,pwd,age});
    }

    private static void test02() throws ClassNotFoundException, SQLException {
        Scanner scanner=new Scanner(System.in);
        System.out.println("输入年龄");
        int age=scanner.nextInt();
        String className="com.mysql.cj.jdbc.Driver";
        String url="jdbc:mysql:///db1";
        String user="root";
        String password="123";
        Class.forName(className);
        Connection connection = DriverManager.getConnection(url, user, password);
        String sql="select * from stu where age=?";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setObject(1, age);
        ResultSet resultSet = statement.executeQuery();
        while(resultSet.next()){
            System.out.println(resultSet.getObject(1));
            System.out.println(resultSet.getObject(2));
            System.out.println(resultSet.getObject(3));
            System.out.println(resultSet.getObject(4));
            System.out.println("-----------------");
        }
        resultSet.close();
        statement.close();
        connection.close();
    }

    private static void test01() throws ClassNotFoundException, SQLException {
        Scanner scanner=new Scanner(System.in);
        System.out.println("输入学号");
        int sid=scanner.nextInt();
        System.out.println("输入姓名");
        String sname=scanner.next();
        System.out.println("输入密码");
        String pwd=scanner.next();
        System.out.println("输入年龄");
        int age=scanner.nextInt();
        String className="com.mysql.cj.jdbc.Driver";
        String url="jdbc:mysql:///db1";
        String user="root";
        String password="123";
        Class.forName(className);
        Connection connection = DriverManager.getConnection(url, user, password);
        //sql语句需要填入输入的参数，使用?当占位符
        String sql="insert into stu values(?,?,?,?)";
        //创建PreparedStatement对象，使用sql语句做参数
        PreparedStatement statement = connection.prepareStatement(sql);
        //给sql语句中?占位符赋值
        statement.setInt(1,sid);
        statement.setString(2,sname);
        statement.setString(3,pwd);
        statement.setObject(4,age);
        //执行sql语句不需要参数
        statement.executeUpdate();
        statement.close();
        connection.close();
    }
}
